package com.jidong.test_springboot.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jidong.test_springboot.entity.Dept;
import com.jidong.test_springboot.module.DeptModule;
import com.jidong.test_springboot.service.DeptService;
import com.jidong.test_springboot.vo.ResultData;

import lombok.extern.slf4j.Slf4j;


@Controller
@RequestMapping("dept/") 
@Slf4j  
public class DeptServlet {
	
	@Autowired
	private DeptService deptService;

	@RequestMapping("add.do")
	@ResponseBody
	@RequiresPermissions({"dept:add"})
	public ResultData add(Dept dept,HttpSession session,ResultData result) throws IOException, ServletException {
		
		int r = deptService.insert(dept);
		if (r > 0) {
			return result.setCode(200).setMsg("添加成功");
		} else {
			return result.setCode(200).setMsg("添加失败");
		}
	}

	@RequestMapping("list.do")
	@ResponseBody
	//@RequiresPermissions({"dept:list"})
	public ResultData list(DeptModule deptModule,ResultData result)  {
		//System.out.println(deptModule);//java对象用输出语句测试，必须重写toString方法
		QueryWrapper<Dept> queryWrapper=new QueryWrapper<>();
		if(deptModule.getDeptLeader()!=null) {
			// like是模糊查询， deptLeader是数据库字段  
			queryWrapper.like("deptLeader", deptModule.getDeptLeader());
		}
		IPage<Dept> page=new Page(deptModule.getPageNo(),deptModule.getPageSize()); 
		IPage<Dept> pages=deptService.list(page, queryWrapper);
		List<Dept> data=pages.getRecords();//数据
		int pageNo=(int)pages.getCurrent();//当前页
		int totalCount=(int)pages.getTotal();//总条数
		
		
		JSONObject ooo=new JSONObject();
    	ooo.put("data", data);//数据
    	ooo.put("totalCount", totalCount);// 总数据条数
    	ooo.put("pageNo", pageNo);//当前页码
    	
    	return result.setCode(200).setMsg("ok").setResult(ooo);
		
	}

	@RequestMapping("update.do")
	@ResponseBody
	@RequiresPermissions({"dept:update"})
	public ResultData update(Dept dept,HttpSession session, HttpServletResponse response) throws IOException, ServletException {
		ResultData result=new ResultData();
		int r=deptService.updateById(dept);
		if(r>0) {
			result.setCode(200);
			result.setMsg("删除成功");
			return result;
		}else {
			result.setCode(200);
			result.setMsg("删除失败");
			return result;
		}
		
	}
	@RequestMapping("delete.do")
	@ResponseBody
	@RequiresPermissions({"dept:delete"})
	public ResultData delete(int id, HttpServletResponse response,HttpSession session) throws IOException, ServletException {
		ResultData result=new ResultData();
		
		int r=deptService.deleteById(id);
		if(r>0) {
			result.setCode(200);
			result.setMsg("删除成功");
			return result;
		}else {
			result.setCode(200);
			result.setMsg("删除失败");
			return result;
		}
	}

}
